package kuang.juc.single;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//反射无法破坏枚举
public enum EnumSingle {
    INSTANCE;

    public void test(){
        System.out.println("测试");
    }

    public EnumSingle getInstance(){
        return INSTANCE;
    }
}
class test2{
    public static void main(String[] args) {
        EnumSingle instance = EnumSingle.INSTANCE;
        instance.test();
    }
}

//class test{
//    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        //反编译源码，枚举的构造器是有参构造
//        Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
//        declaredConstructor.setAccessible(true);
//        //Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
//        //无法反射破解
//        EnumSingle enumSingle = declaredConstructor.newInstance();
//        System.out.println(enumSingle.getInstance());
//    }
//}
